package com.jiutong.meipenggang.ui.fragments;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.BaseApplication;
import com.jiutong.meipenggang.base.BaseFragment;
import com.jiutong.meipenggang.base.CommonAdapter;
import com.jiutong.meipenggang.base.ViewHolder;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.HomeHongBaoBean;
import com.jiutong.meipenggang.http.model.interfaces.HongBaoBiz;
import com.jiutong.meipenggang.http.present.HongBaoPresent;
import com.jiutong.meipenggang.utils.NumUilt;
import com.jiutong.meipenggang.utils.SpConfig;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * @author Veken
 * @date on 2017/12/23 18:15
 * @describe
 */

public class UsedHongBaoFragment extends BaseFragment implements HongBaoBiz {
    @Bind(R.id.hongbao_item_List)
    ListView hongbaoItemList;


    private HongbaoAdapter adapter;

    private List<HomeHongBaoBean.RedListBean> redList = new ArrayList<>();
    private HongBaoPresent hongBaoPresent;

    private boolean isInit = false;

    @Override
    protected int getLayoutResouceId() {
        return R.layout.fragment_hongbao;
    }

    @Override
    protected void onLazyLoad() {
        super.onLazyLoad();
        if (!isInit) {
            hongBaoPresent = new HongBaoPresent(mActivity, this);
            hongBaoPresent.getHomeHongBaoInfo("2");
            isInit = true;
        } else {
            showData();
        }
    }



    @Override
    public String redState() {
        return null;
    }

    @Override
    public void onSuccess(Object object) {
        HomeHongBaoBean bean = (HomeHongBaoBean) object;
        if (bean.getError() == -1) {
            redList = bean.getRedList();
            adapter = new HongbaoAdapter(mActivity, redList, R.layout.hongbao_item_list);
            hongbaoItemList.setAdapter(adapter);
            showData();
            SpConfig.getInstance().edit().putString(Constants.ID, bean.getId()).commit();
            SpConfig.getInstance().edit().putString(Constants.TOKEN, bean.getToken()).commit();
        } else {
            Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 显示数据
     */
    private void showData() {
        if (redList.size() <= 0) {
            if (isInit) {
                toastShortShow("没有已使用的红包");
            }
        } else {
           adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }

    /**
     * 获取红包列表的Adapter
     */
    public class HongbaoAdapter extends CommonAdapter<HomeHongBaoBean.RedListBean> {

        private TextView tv_item_hongbao_money_yuan;
        private TextView tv_item_hongbao_money;
        private TextView tv_hongbao_describe;
        private TextView tv_item_hongbao_reward_money;
        private TextView tv_item_hongbao_limitday;
        private RelativeLayout rl_item_hongbao;

        public HongbaoAdapter(Context context, List<HomeHongBaoBean.RedListBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, final int position) {
            tv_item_hongbao_money_yuan = holder.getView(R.id.tv_item_hongbao_money_yuan);
            tv_item_hongbao_money = holder.getView(R.id.tv_item_hongbao_money);
            tv_hongbao_describe = holder.getView(R.id.tv_hongbao_describe);
            tv_item_hongbao_reward_money = holder.getView(R.id.tv_item_hongbao_reward_money);
            tv_item_hongbao_limitday = holder.getView(R.id.tv_item_hongbao_limitday);
            rl_item_hongbao = holder.getView(R.id.rl_item_hongbao);
            tv_item_hongbao_money.setTextColor(getResources().getColor(R.color.hongbao_gray));
            tv_item_hongbao_money_yuan.setTextColor(getResources().getColor(R.color.hongbao_gray));
            tv_item_hongbao_reward_money.setBackgroundResource(R.drawable.shape_hongbao_list_gray_bg);
            rl_item_hongbao.setBackgroundResource(R.mipmap.hongbao_gray);
            tv_hongbao_describe.setTextColor(getResources().getColor(R.color.hongbao_dark));
            tv_item_hongbao_limitday.setTextColor(getResources().getColor(R.color.hongbao_dark));

            //红包金额
            tv_item_hongbao_money.setText(NumUilt.formatInt(String.valueOf(listDatas.get(position).getRedAmount())) + "");
            //红包奖励规则金额，即满多少可用
            tv_item_hongbao_reward_money.setText("满" + listDatas.get(position).getRedRuleAmount() + "可用");
            //红包描述
            tv_hongbao_describe.setText("红包(" + listDatas.get(position).getRedName() + ")");
            //有效期
            tv_item_hongbao_limitday.setText("有效期至" + listDatas.get(position).getEndTime());
        }
    }
}

